package com.ziroom.crm.mq.core.processor;

import com.ziroom.crm.mq.core.spi.SpiLoader;
import com.ziroom.crm.mq.core.spi.SpiOrder;

import java.util.List;
import java.util.Objects;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author WeiShaoying
 * @version 1.0
 * @date Created in 2020年09月01日 17:44
 * @since 1.0
 */
public class ProcessorFactory {

    /**
     * 发送处理类
     */
    private static List<SendProcessor> sendProcessorList;

    /**
     * 重新发送处理类
     */
    private static List<ReSendProcessor> reSendProcessorList;

    /**
     * 消费处理类
     */
    private static List<ConsumeProcessor> consumeProcessorList;

    /**
     * 重新消费处理类
     */
    private static List<ReConsumeProcessor> reConsumeProcessorList;

    /**
     * 发送锁
     */
    private static final Object SEND_LOCK = new Object();
    /**
     * 重新发送锁
     */
    private static final Object RE_SEND_LOCK = new Object();

    /**
     * 消费锁
     */
    private static final Object CONSUME_LOCK = new Object();
    /**
     * 重新消费锁
     */
    private static final Object RE_CONSUME_LOCK = new Object();

    /**
     * 获取发送处理类
     *
     * @return
     */
    public static List<SendProcessor> getSendProcessorList() {
        if (Objects.isNull(sendProcessorList)) {
            synchronized (SEND_LOCK) {
                if (Objects.isNull(sendProcessorList)) {
                    sendProcessorList = SpiLoader.loadPrototypeInstanceListSorted(SendProcessor.class);
                }
            }
        }
        return sendProcessorList;
    }

    /**
     * 获取重发处理类
     *
     * @return
     */
    public static List<ReSendProcessor> getReSendProcessorList() {
        if (Objects.isNull(reSendProcessorList)) {
            synchronized (RE_SEND_LOCK) {
                if (Objects.isNull(reSendProcessorList)) {
                    reSendProcessorList = SpiLoader.loadPrototypeInstanceListSorted(ReSendProcessor.class);
                }
            }
        }
        return reSendProcessorList;
    }

    /**
     * 获取发送处理类
     * @return
     */
    public static List<ConsumeProcessor> getConsumeProcessorList() {
        if (Objects.isNull(consumeProcessorList)) {
            synchronized(CONSUME_LOCK) {
                if (Objects.isNull(consumeProcessorList)) {
                    consumeProcessorList = SpiLoader.loadPrototypeInstanceListSorted(ConsumeProcessor.class);
                }
            }
        }
        return consumeProcessorList;
    }

    /**
     * 获取重发处理类
     * @return
     */
    public static List<ReConsumeProcessor> getReConsumeProcessorList() {
        if (Objects.isNull(reConsumeProcessorList)) {
            synchronized(RE_CONSUME_LOCK) {
                if (Objects.isNull(reConsumeProcessorList)) {
                    reConsumeProcessorList = SpiLoader.loadPrototypeInstanceListSorted(ReConsumeProcessor.class);
                }
            }
        }
        return reConsumeProcessorList;
    }
}
